package main

import (
	"fmt"
	"sync"

	"gitee.com/technology-to-share/concurrency-of-shared-variables/example4/bank"
)

func main() {
	for i := 0; i < 10000; i++ {
		test()
	}
}

//使用容量为1的channel，实现锁
func test() {
	bank.Init()
	var wg sync.WaitGroup
	wg.Add(2)
	// A：
	go func() {
		defer wg.Done()
		bank.Add(200)                    // A1
		fmt.Println("=", bank.Balance()) // A2
	}()

	// B:
	go func() {
		defer wg.Done()
		bank.Add(100) // B
	}()
	wg.Wait()
}
